$(()=>{
	$('.loading').css('display','none')
})
	//初始化时间标题
	$('#date-title').text(getDate(new Date()))
	//记录当前点击的时间按钮
	let currentClickedBtn = $('#date-title').text()
	//给每个任务一个id，每次添加任务时加1并赋值给任务
	let taskId = 0
	taskId = localStorage.getItem('taskId')
	// 记录是否展示未完成
	let isShowUnFinished = false
	
	//日期选择
	// function getDataBtnHandle(event){
	// 	console.log(event.target)
	// 	$(event.target).datepicker()
	// }
	
	// function dateChange(e){
	// 	var dateVal = $(e.target).val()
	// 	// console.log(new Date().setTime(dateVal))
	// 	$(e.target).val('选择日期')
	// 	$('#date-title').text(dateVal)
	// }

	//点击图标切换到表单
	$('.icon').on('click',()=>{
		$('.icon').css('display','none')
		$('.footer form').slideToggle('slow') //表单滑入
	})
	
	var dateObj = {
	weekArr:[],
	dateArr:[]
	}
	//循环得到一个时间对象
	for(var i=-1;i<4;i++){
		var tmpTime = new Date(Date.now()+i*24*60*60*1000)
		tmpWeek = changeWeek(tmpTime.getDay())
		dateObj.weekArr.push(tmpWeek)
		dateObj.dateArr.push(getDate(tmpTime))
	}
	
	//刷新时间按钮组
	refreshDateBtn(dateObj)
	
	//模板引擎渲染
	function refreshDateBtn(tempData){
		var htmlStr = template('t1',tempData)	
		$('#btns').html(htmlStr)
	}
	
	//点击日期按钮函数
	$('#btns button').click(function(event){ 
		var $e = $(event.target)
		currentClickedBtn = $e.attr('date-id')
		//根据button的date-id属性
		//更改时间标题
		$('#date-title').text($e.attr('date-id'))
		//刷新任务列表
		getData()
	})
	
	//判断localstorage中有没有数据
	var taskArr 
	var taskStr = localStorage.getItem('task')
	if(taskStr){
		taskArr = JSON.parse(taskStr)
	}else{
		taskArr = []
	}
	
	//点击表单添加按钮
	$('#addTask').on('click',()=>{
		var timeStamp = currentClickedBtn
		if(timeStamp){
			taskId ++
			saveData(timeStamp,taskId)
			getData()
			$('#taskText').val('')
		}
		else{
			layer.msg('请选择日期')
		}		
	})
	//排序
	$( "#sortable" ).sortable({
		update : function(event,ui){
			var taskData = JSON.parse(localStorage.getItem('task'))
			var newSortArr = []		
			$(this).children().each(function(){
				for(var j=0;j<taskData.length;j++){
					var newSortObj = {}
					if(taskData[j].taskId==this.id &&　taskData[j].date==currentClickedBtn){
						newSortObj.date = taskData[j].date
						newSortObj.task = taskData[j].task
						newSortObj.taskId = taskData[j].taskId
						newSortObj.isFinished = taskData[j].isFinished
						newSortArr.push(newSortObj)
						taskData.splice(j,1)
					}
				}
			})
			newSortArr.forEach(function(item){
				taskData.push(item)
			})
			localStorage.removeItem('task')
			localStorage.setItem('task',JSON.stringify(taskData))
			taskArr = taskData
			getData()
    	}
	})
    
	//删除
	function signFinish(e){
		let src = $(e.target).attr('src')
		let selectImg = 'img/select.png'
		let unselectImg = 'img/unselect.png'
		let taskId = $(e.currentTarget).parent().attr('id')
		taskArr.forEach(item=>{
			if(item.taskId==taskId && src==unselectImg){
				$(e.target).attr('src',selectImg)
				item.isFinished = true
			}
			if(item.taskId==taskId && src==selectImg){
				$(e.target).attr('src',unselectImg)
				item.isFinished = false
			}
		})
		localStorage.removeItem('task')
		localStorage.setItem('task',JSON.stringify(taskArr))
		taskArr = JSON.parse(localStorage.getItem('task'))
		getData()
	}
	// 切换已完成，未完成
	function showFinish(e){
		
		if(!isShowUnFinished){
			isShowUnFinished = true
			$(e.target).text('隐藏已完成')
		}	else{
			isShowUnFinished = false
			$(e.target).text('显示已完成')
		}
		getData()
	}
	// 显示完成的数据
	//存数据
	function saveData(date){
		var taskVal = $('#taskText').val()	
		var taskObj = {}
		if(taskVal){
			taskObj.date = date
			taskObj.task = taskVal
			taskObj.taskId = taskId
			taskObj.isFinished = false
			taskArr.push(taskObj)
		}else{
			layer.tips('内容不能为空', '#taskText', {
			  tips: [1, '#f00'],
			  time: 4000
			})
		}
		localStorage.setItem('task',JSON.stringify(taskArr))
		localStorage.setItem('taskId',taskId)
	}
	
	//取数据
	function getData(){
		let unfinishedArr = taskArr.filter(function(i){
			return i.date==currentClickedBtn && i.isFinished ==false
		})
		$('#unfinishNum').text(unfinishedArr.length+'项未完成')
		var localDataObj = {
			data:taskArr,
			time:currentClickedBtn,
			taskId:taskId,
			isShow:isShowUnFinished
		}

		var htmlStr = template('t2',localDataObj)	
		$('.task-list').html(htmlStr)
	}
	getData()	
	//转化周一，周二，.....
	function changeWeek(tmpWeek){
		switch (tmpWeek){
			case 0:
			tmpWeek = '周日'
			break
			case 1:
			tmpWeek = '周一'
			break
			case 2:
			tmpWeek = '周二'
			break
			case 3:
			tmpWeek = '周三'
			break
			case 4:
			tmpWeek = '周四'
			break
			case 5:
			tmpWeek = '周五'
			break
			case 6:
			tmpWeek = '周六'
			break
		}
		return tmpWeek
	}
	
	//获得年，月，日，周
	function getDate(time){
		let year = time.getFullYear()
		let month = time.getMonth()+1
		let day = time.getDate()
		let week = changeWeek(time.getDay())
		const timeStr = year + '年' + month + '月' + day + '日' +' ' + week
		return timeStr
	}

